﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace iopt.solvency.pillarone.client.desktop.Views
{
    /// <summary>
    /// Logique d'interaction pour EntityDesigner.xaml
    /// </summary>
    public partial class EntityDesigner : UserControl
    {
        public EntityDesigner()
        {
            InitializeComponent();
        }

        private void DataGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.V &&
                Keyboard.Modifiers == ModifierKeys.Control)
            {
                try
                {
                    var data = (string)Clipboard.GetData(DataFormats.Text);

                    string[][] clipboardData =
                        ((string)Clipboard.GetData(DataFormats.Text)).Split('\n')
                        .Select(row =>
                                row.Split('\t')
                                .Select(cell =>
                                        cell.Length > 0 && cell[cell.Length - 1] == '\r' ?
                                        cell.Substring(0, cell.Length - 1) : cell).ToArray())
                        .Where(a => a.Any(b => b.Length > 0)).ToArray();

                    var vm = this.DataContext as iopt.solvency.pillarone.client.desktop.ViewModels.VMEntityDesigner;

                    vm.Model.DefaultEntity.Properties.Clear();

                    foreach (var element in clipboardData)
                    {
                        var prop = new iopt.solvency.pillarone.client.desktop.ViewModels.ModelPropertyDefinition();
                        prop.Name = element[0];

                        prop.Type =
                            element[1] == "Numérique" || element[1] == "num" || element[1] == "Num." ? iopt.solvency.pillarone.client.desktop.ViewModels.ModelParamUnderLyingType.Double :
                            element[1] == "Alphanumérique" || element[1] == "char" || element[1] == "Texte" ? iopt.solvency.pillarone.client.desktop.ViewModels.ModelParamUnderLyingType.Case :
                            element[1] == "Date"  ? iopt.solvency.pillarone.client.desktop.ViewModels.ModelParamUnderLyingType.Date :
                            iopt.solvency.pillarone.client.desktop.ViewModels.ModelParamUnderLyingType.Mixed;


                        vm.Model.DefaultEntity.Properties.Add(prop);
                    }
                }
                catch (Exception)
                {
                    
                }
            }
        }
    }
}
